package diona.clipboard;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * User: anton
 * Date: 20.03.11 12:30
 */
public class ClipboardHelper
{
    private static Logger LOG = LoggerFactory.getLogger(ClipboardHelper.class);

    public static ClipboardItem getClipboardItem()
    {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        while (true) {
            try {
                Transferable contents = clipboard.getContents(null);
                if (contents.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    return new ImageItem((BufferedImage) contents.getTransferData(DataFlavor.imageFlavor));
                }
                if (contents.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    //noinspection unchecked
                    return new FileListItem(
                            (java.util.List<File>) contents.getTransferData(DataFlavor.javaFileListFlavor));
                }
                return null;
            } catch (Exception e) {
                LOG.warn("Cannot get clipboard content", e);
            }
        }
    }
}
